//
//  BKUserMyAccountController.m
//  bestkeep
//
//  Created by windCould on 16/12/19.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserMyAccountController.h"
#import "BKUserUnitRequestAPI.h"
#import "BKMyCommon.h"

@interface BKUserMyAccountController ()

@property(nonatomic,strong)UIView * bGView;//白色视图
@property(nonatomic,strong)UIImageView * goldenImagV;//钱图片
@property(nonatomic,strong)UILabel *account;//我的余额
@property(nonatomic,strong)UILabel *amount;//金额
@property (nonatomic,strong) ExceptionView * exceptionView;

@end

@implementation BKUserMyAccountController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"我的账户";
    self.navigationController.navigationBar.hidden=NO;
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
    }

    self.view.backgroundColor =[UIColor whiteColor];
   
    [self nonVipView];
 
}

//判断是否是会员
-(void)viplevel{
    
    __weak typeof(self) wSelf =self;
    if(self.exceptionView) {
        
        [self.exceptionView dismiss];
        self.exceptionView = nil;
    }
    
    __weak typeof(self) weakSelf = self;
    [BKIndicationView showInView:self.view];
    [BKUserUnitRequestAPI getUserAccountDetailWithBlock:^(id obj, NSError *error) {
        
        [BKIndicationView dismiss];
        if (obj) {
            
            self.bGView.hidden = NO;
            NSDictionary * dict = obj;
            BOOL success = [[dict  objectForKey:@"success"] boolValue];
            if (success) {
               
                NSArray * dataArr = [[dict objectForKey:@"data"] objectForKey:@"account"];
                for (NSDictionary * temDict in dataArr) {
                    
                    if ([temDict[@"account_type"] isEqualToString:@"002"]) {
                        
                        NSLog(@"$$$$$%f",[temDict[@"account_balance"] floatValue]);
                        wSelf.amount.text = [NSString stringWithFormat:@"¥%@",temDict[@"account_balance"]];
                        NSLog(@"%@",self.amount.text);
                    }
                }
            } else {
            
                [HUD showMessage: dict[@"msg"]];
            }
        } else {
            
            if(!self.exceptionView && error) {
                
                if(!self.exceptionView && error) {
                    
                    ExceptionCodeType errorType = ExceptionCodeTypeRequestFailed;
                    if (error.code == -1009) {
                        errorType = ExceptionCodeTypeNoNetwork;
                    }else if (error.code == -1001){
                        errorType = ExceptionCodeTypeRequestFailed;
                    }else if (error.code == 502 || error.code == 404){
                        errorType = ExceptionCodeTypeServerError;
                    }else{
                        errorType = ExceptionCodeTypeFailedToLoad;
                    }
                    
                    weakSelf.exceptionView = [ExceptionView exceptionViewWithType:errorType];
                    ExceptionViewAction *retryAction = [ExceptionViewAction actionWithType:errorType handler:^(ExceptionViewAction *action) {
                        
                        [weakSelf viplevel];
                    }];
                    [self.exceptionView addAction:retryAction];
                    
                    [self.exceptionView showInView:self.view];

                    weakSelf.exceptionView.frame = CGRectMake(0, -64, self.view.frame.size.width, self.view.frame.size.height+64);
            }
         }
        }  //  [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.3f];
    }];
    
    
}

-(void)delayMethod{
    
   // self.bGView.hidden = NO;
}


//非会员界面
-(void)nonVipView{
   
    self.bGView=[[UIView alloc]initWithFrame:CGRectZero];
    self.bGView.hidden = YES;
    self.bGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.bGView];
    
      
    self.goldenImagV = [[UIImageView alloc]initWithFrame:CGRectZero];
    UIImage * image = [UIImage imageNamed:@"BK_My_myAccount Copy"];
    [self.goldenImagV setImage:image];
    
    [self.bGView addSubview:self.goldenImagV];
    

    self.account=[[UILabel alloc]initWithFrame:CGRectZero];
    self.account.font=[UIFont systemFontOfSize:15];
    self.account.textColor=BK_MY_COLOR_04;
    [self.bGView addSubview:self.account];
    

    self.account.text =@"账户余额";
    self.amount = [[UILabel alloc]initWithFrame:CGRectZero];
    self.amount.font =[UIFont systemFontOfSize:15];
    
    [self viplevel];
    
    self.amount.textColor=HexColor(0xD74545);
    [self.bGView addSubview:self.amount];
    
    [self layoutViews];
}

-(void)layoutViews
{
    
    [self.bGView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top);
        make.height.equalTo(@(self.view.frame.size.height/3));
    }];
    
    
     UIImage * image = [UIImage imageNamed:@"BK_My_myAccount Copy"];
    [self.goldenImagV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.bGView.mas_top).mas_offset(36);
        make.centerX.equalTo(self.bGView.mas_centerX);
        make.width.equalTo(@(image.size.width));
        make.height.equalTo(@(image.size.height));
    }];
    
    
    [self.account mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.bGView.mas_centerX);
        make.top.equalTo(self.goldenImagV.mas_bottom).mas_offset(14);
    }];
    
    
    [self.amount mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.bGView.mas_centerX);
        make.top.equalTo(self.account.mas_bottom).mas_offset(10);
    
    }];
}


@end
